package com.c3w.synchro.member.validator;

import com.c3w.synchro.common.dao.BeanDao;
import com.c3w.synchro.common.validation.ValidationResult;
import com.c3w.synchro.common.validation.Validator;
import com.c3w.synchro.common.validation.event.ValidationFailedException;
import com.c3w.synchro.member.bean.Coach;
import org.apache.commons.validator.GenericValidator;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * User: magirard
 * Date: 13-02-13
 */
public class CoachValidator implements Validator<Coach> {
    @Autowired
    private BeanDao<Coach> coachDao;

    @Override
    public void validate(Coach bean) throws ValidationFailedException {
        ValidationResult result = new ValidationResult();

        // Verifie l'existance du bean
        if (bean != null) {
            // 1- Validation de l'athlete
            String name = bean.getName();
            if (name == null) {
                result.add("name", "Name be provided");
            } else if (!GenericValidator.minLength(name, 6)) {
                result.add("name", "name must have a least 6 character");
            }

        } else {
            result.add("Coach", "Coach n'existe pas");
        }

        if (result.getErrorCount() > 0) {
            throw new ValidationFailedException(result);
        }
    }
}
